import pygame.font 


class  Button:
    """为游戏构建按钮的类."""

    def __init__(self, ai_game, msg, width=200, 
                 height=50, 
                 button_color=(0, 135, 0), 
                 text_color=(255, 255, 255),
                 font = None):
        """初始化按钮属性."""
        self.screen=ai_game.screen
        self.screen_rect=self.screen.get_rect()

        # 设置按钮的尺寸和属性
        self.width, self.height = width, height 
        self.button_color = button_color 
        self.text_color = text_color 
        self.font = pygame.font.SysFont(font, 48) if font else pygame.font.SysFont("方正舒体", 48) 

        # 构建按钮的Rect对象并将其居中。 
        self.rect = pygame.Rect(0, 0, self.width, self.height) 
        self.rect.center = self.screen_rect.center 

        # 准备一次按钮消息
        self._prep_msg(msg) 

    def _prep_msg(self, msg): 
        """将消息转换为渲染图像并将文本居中显示在按钮上."""
        self.msg_image = self.font.render(msg, True, self.text_color, 
                                          self.button_color) 
        self.msg_image_rect = self.msg_image.get_rect() 
        self.msg_image_rect.center = self.rect.center 

    def draw_button(self): 
        """绘制空白按钮，然后绘制消息.""" 
        self._draw_rect() 
        self._draw_text() 

    def _draw_rect(self): 
        """绘制按钮的矩形背景."""
        self.screen.fill(self.button_color, self.rect) 

    def _draw_text(self): 
        """绘制按钮的文本"""
        self.screen.blit(self.msg_image, self.msg_image_rect) 
